
package fileby.gui.magicbox;

import com.google.inject.Inject;
import fileby.util.properties.IPropertyBundleProvider;
import fileby.util.properties.PropertyBundle;
import fileby.core.ActionKey;
import fileby.core.IDisplayableFile;
import fileby.ctrl.actions.IActionProvider;
import fileby.ctrl.transferhandlers.ITransferHandlerProvider;
import fileby.core.TransferHandlerType;
import fileby.gui.util.ContentPanel;
import fileby.util.plugins.Plugin;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.util.List;
import javax.swing.Icon;
import javax.swing.JScrollPane;

/**
 *
 * @author Ludwig
 */
public class MagicBox extends javax.swing.JPanel {

    private Icon background_north;
    private Icon background_south;
    private ContentPanel contents;
    private IActionProvider actionProvider;
    private IPropertyBundleProvider propertiesProvider;
    private IMagicBoxItemProvider itemProvider;
    private ITransferHandlerProvider transferHandlerProvider;

    @Inject
    public MagicBox(IPropertyBundleProvider propertiesProvider,
            IMagicBoxItemProvider itemProvider,
            IActionProvider actionProvider,
            ITransferHandlerProvider transferHandlerProvider) {

        this.propertiesProvider = propertiesProvider;
        this.actionProvider = actionProvider;
        this.itemProvider = itemProvider;
        this.transferHandlerProvider = transferHandlerProvider;
        initComponents();
        initCtrl();
    }

    public void setPlugin(Plugin p) {
        pluginName.setText(p.getName());
        pluginName.setToolTipText(p.getDescription());
    }

    public void updateItem(List<IDisplayableFile> files) {
        contents.removeAll();
        boolean whiteStripe = false;

        for (IDisplayableFile f : files) {
            MagicBoxItem m = itemProvider.create(f);
            if (whiteStripe)
                m.setBackground(Color.WHITE);
            whiteStripe = !whiteStripe;
            contents.add(m);
        }
        contents.validate();
        validate();
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        background_north.paintIcon(northPanel, g, 5, 0);
        g.drawLine(5, 30, 5, southPanel.getY());
        g.drawLine(199, 30, 199, southPanel.getY());
        background_south.paintIcon(southPanel, g, 5, southPanel.getY());
    }

    private void initCtrl() {
        loadButton.setAction(actionProvider.getControl(ActionKey.LOAD_PLUGIN));
        executeButton.addActionListener(actionProvider.getControl(ActionKey.EXECUTE_MAGIC_BOX));
        exportButton.addActionListener(actionProvider.getControl(ActionKey.EXPORT_MAGIC_BOX));
        importButton.addActionListener(actionProvider.getControl(ActionKey.IMPORT_MAGIC_BOX));
        clearButton.addActionListener(actionProvider.getControl(ActionKey.CLEAR_MAGIC_BOX));
        setTransferHandler(transferHandlerProvider.create(TransferHandlerType.MAGIC_BOX));
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        contents = new ContentPanel();
        centerPanel = new javax.swing.JPanel();
        eastPanel = new javax.swing.JPanel();
        exportButton = new javax.swing.JButton();
        importButton = new javax.swing.JButton();
        clearButton = new javax.swing.JButton();
        buttonPanel = new javax.swing.JPanel();
        loadButton = new javax.swing.JButton();
        executeButton = new javax.swing.JButton();
        southPanel = new javax.swing.JPanel();
        northPanel = new javax.swing.JPanel();
        pluginName = new javax.swing.JLabel();
        title = new javax.swing.JLabel();

        PropertyBundle bundle = propertiesProvider.create(this.getClass());
        setBackground(bundle.getColor("Form.background")); // NOI18N
        setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 4, 4, 0));
        setMaximumSize(new java.awt.Dimension(200, 32767));
        setMinimumSize(new java.awt.Dimension(200, 100));
        setOpaque(false);
        setPreferredSize(new java.awt.Dimension(200, 200));
        background_north = bundle.getIcon("background.north");
        background_south = bundle.getIcon("background.south");

        centerPanel.setBackground(bundle.getColor("centerPanel.background")); // NOI18N
        centerPanel.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        centerPanel.setMaximumSize(new java.awt.Dimension(157, 32767));
        centerPanel.setMinimumSize(new java.awt.Dimension(157, 10));
        centerPanel.setOpaque(false);
        centerPanel.setPreferredSize(new java.awt.Dimension(150, 96));
        centerPanel.setLayout(new java.awt.BorderLayout());

        eastPanel.setBackground(bundle.getColor("eastPanel.background")); // NOI18N
        eastPanel.setMaximumSize(new java.awt.Dimension(40, 32767));
        eastPanel.setMinimumSize(new java.awt.Dimension(40, 50));
        eastPanel.setPreferredSize(new java.awt.Dimension(40, 50));

        exportButton.setText(bundle.getString("exportButton.text")); // NOI18N
        exportButton.setAlignmentY(0.0F);
        exportButton.setMargin(new java.awt.Insets(0, 0, 0, 0));

        importButton.setText(bundle.getString("importButton.text")); // NOI18N
        importButton.setAlignmentY(0.0F);
        importButton.setMargin(new java.awt.Insets(0, 0, 0, 0));

        clearButton.setText(bundle.getString("clearButton.text")); // NOI18N
        clearButton.setAlignmentY(0.0F);
        clearButton.setMargin(new java.awt.Insets(0, 0, 0, 0));

        javax.swing.GroupLayout eastPanelLayout = new javax.swing.GroupLayout(eastPanel);
        eastPanel.setLayout(eastPanelLayout);
        eastPanelLayout.setHorizontalGroup(
            eastPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, eastPanelLayout.createSequentialGroup()
                .addGroup(eastPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(exportButton, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 40, Short.MAX_VALUE)
                    .addComponent(importButton, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 40, Short.MAX_VALUE)
                    .addComponent(clearButton, javax.swing.GroupLayout.DEFAULT_SIZE, 40, Short.MAX_VALUE))
                .addGap(0, 0, 0))
        );
        eastPanelLayout.setVerticalGroup(
            eastPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(eastPanelLayout.createSequentialGroup()
                .addContainerGap(42, Short.MAX_VALUE)
                .addComponent(exportButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(importButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 31, Short.MAX_VALUE)
                .addComponent(clearButton))
        );

        buttonPanel.setBackground(bundle.getColor("buttonPanel.background")); // NOI18N
        buttonPanel.setMaximumSize(new java.awt.Dimension(193, 25));
        buttonPanel.setMinimumSize(new java.awt.Dimension(193, 25));
        buttonPanel.setPreferredSize(new java.awt.Dimension(193, 25));

        loadButton.setText(bundle.getString("loadButton.text")); // NOI18N

        executeButton.setText(bundle.getString("executeButton.text")); // NOI18N

        javax.swing.GroupLayout buttonPanelLayout = new javax.swing.GroupLayout(buttonPanel);
        buttonPanel.setLayout(buttonPanelLayout);
        buttonPanelLayout.setHorizontalGroup(
            buttonPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, buttonPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(loadButton, javax.swing.GroupLayout.DEFAULT_SIZE, 92, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(executeButton, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        buttonPanelLayout.setVerticalGroup(
            buttonPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(buttonPanelLayout.createSequentialGroup()
                .addGroup(buttonPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(loadButton)
                    .addComponent(executeButton))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        southPanel.setMaximumSize(new java.awt.Dimension(200, 5));
        southPanel.setMinimumSize(new java.awt.Dimension(200, 5));
        southPanel.setOpaque(false);
        southPanel.setPreferredSize(new java.awt.Dimension(200, 5));

        javax.swing.GroupLayout southPanelLayout = new javax.swing.GroupLayout(southPanel);
        southPanel.setLayout(southPanelLayout);
        southPanelLayout.setHorizontalGroup(
            southPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 202, Short.MAX_VALUE)
        );
        southPanelLayout.setVerticalGroup(
            southPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 5, Short.MAX_VALUE)
        );

        northPanel.setMaximumSize(new java.awt.Dimension(200, 30));
        northPanel.setMinimumSize(new java.awt.Dimension(200, 30));
        northPanel.setOpaque(false);
        northPanel.setPreferredSize(new java.awt.Dimension(200, 30));

        pluginName.setFont(bundle.getFont("pluginName.font")); // NOI18N
        pluginName.setText(bundle.getString("pluginName.text")); // NOI18N

        title.setFont(bundle.getFont("title.font")); // NOI18N
        title.setText(bundle.getString("title.text")); // NOI18

        javax.swing.GroupLayout northPanelLayout = new javax.swing.GroupLayout(northPanel);
        northPanel.setLayout(northPanelLayout);
        northPanelLayout.setHorizontalGroup(
            northPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, northPanelLayout.createSequentialGroup()
                .addContainerGap(23, Short.MAX_VALUE)
                .addComponent(title, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, 0)
                .addComponent(pluginName, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        northPanelLayout.setVerticalGroup(
            northPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(northPanelLayout.createSequentialGroup()
                .addGroup(northPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(title, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(pluginName, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(southPanel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 202, Short.MAX_VALUE)
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addGap(2, 2, 2)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(northPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(centerPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 153, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGap(0, 0, 0)
                                    .addComponent(eastPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(2, 2, 2)
                        .addComponent(buttonPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(21, 21, 21))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(northPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, 0)
                .addComponent(buttonPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, 0)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(centerPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 136, Short.MAX_VALUE)
                    .addComponent(eastPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 136, Short.MAX_VALUE))
                .addGap(0, 0, 0)
                .addComponent(southPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        FlowLayout flowLayout = new FlowLayout(FlowLayout.LEFT, 0, 0);
        contents.setLayout(flowLayout);
        JScrollPane scroll = new JScrollPane(contents);
        centerPanel.add(scroll, BorderLayout.CENTER);

    }// </editor-fold>

    private javax.swing.JPanel buttonPanel;
    private javax.swing.JPanel centerPanel;
    private javax.swing.JButton clearButton;
    private javax.swing.JPanel eastPanel;
    private javax.swing.JButton executeButton;
    private javax.swing.JButton exportButton;
    private javax.swing.JButton importButton;
    private javax.swing.JButton loadButton;
    private javax.swing.JPanel northPanel;
    private javax.swing.JLabel pluginName;
    private javax.swing.JPanel southPanel;
    private javax.swing.JLabel title;

}
